package cn.eng.system.controller;

import cn.eng.basic.util.AjaxResult;
import cn.eng.basic.util.PageList;
import cn.eng.system.domain.Dictionarydetail;
import cn.eng.system.query.DictionarydetailQuery;
import cn.eng.system.service.IDictionarydetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 接口: 字典明细 
 */
@RestController
@RequestMapping("/dictionarydetail")
public class DictionarydetailController {
    @Autowired
    private IDictionarydetailService dictionarydetailService;
    /**
     * 查询所有对象
     * @return
     */
    @GetMapping
    public List<Dictionarydetail> loadAll(){
        return dictionarydetailService.loadAll();
    }
    /**
     * 查询单个对象
     * @return
     */
    @GetMapping("/{id}")
    public Dictionarydetail loadById(@PathVariable("id") Long id){
        return dictionarydetailService.loadById(id);
    }

    /**
     * 分页查询
     * @param dictionarydetailQuery
     * @return
     */
    @PostMapping
    public PageList<Dictionarydetail> queryPage(@RequestBody DictionarydetailQuery dictionarydetailQuery){
        return dictionarydetailService.queryPage(dictionarydetailQuery);
    }

    /**
     * 单个删除接口
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable("id") Long id){
        try {
            dictionarydetailService.remove(id);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"删除失败");
        }
    }

    /**
     * 添加或修改接口
     * @param dictionarydetail
     * @return
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Dictionarydetail dictionarydetail){
        try {
            if (dictionarydetail.getId()==null){
                dictionarydetailService.save(dictionarydetail);
            }else {
                dictionarydetailService.update(dictionarydetail);
            }
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"操作失败");
        }
    }

    /**
     * 批量删除接口
     * @param ids
     * @return
     */
    @PatchMapping
    public AjaxResult patchDel(@RequestBody Long[] ids){
        try {
            dictionarydetailService.patchDel(ids);
            return  new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"批量删除失败");
        }

    }
}
